package com.scalar.chiptrack.users;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DBXException;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.operations.reports.DataDumpReportsHelper;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.Mailer;
import com.scalar.ScalarException;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.io.FileOutputStream;
import java.io.File;

/**

 * User: Ganesh
 * Date: Jun 24, 2008
 * Time: 7:10:26 PM
 */
public class ExpiryUsersReportJob extends JobHandler {

    public void execute() throws Exception
    {
        DBTransaction dbTranasaction    = null;
        boolean success                 = false;
        Workbook workbook           = null;
        String fileName                 = "";
        ArrayList emailsList             = new ArrayList();
        try
        {
            String[] mailAttachments    = null;
            String[] emailArray         = null;

            try
            {
                emailsList = UserDAO.loadEmailsByGroupName (null, Constants.USER_GROUP_ADMIN);

                if (emailsList != null)
                {
                    emailArray = (String[]) emailsList.toArray(new String[0]);

                    if ((emailArray != null) && (emailArray.length > 0))
                    {
                        Arrays.sort(emailArray);
                    }
                }
            }
            catch (ScalarException se)
            {
                se.getRootCause().printStackTrace();
                System.out.println("Exception while retrieving email list");
            }

            ArrayList usersList = UserDAO.loadExpiryUsersReprot(dbTranasaction);
            workbook = DataDumpReportsHelper.getWorkbook(usersList, "Users Expiry Information Report", null,null,null);

            FileOutputStream fileOut;
            String excelFilePath = null;
            excelFilePath = ConfigManager.getExpiryUsersReportDirectory();
            boolean exists = (new File(excelFilePath)).exists();

            if (!exists)
            {
                File file = new File(excelFilePath);
                file.mkdirs();
            }

            Calendar calendar = new GregorianCalendar();
            fileName = "UsersExpiryInformationReport " + (calendar.get(Calendar.MONTH) + 1) + "_" + (calendar.get(Calendar.DATE)) + "_" + calendar.get(Calendar.YEAR) ;
            fileOut = new FileOutputStream(excelFilePath + "/" + fileName);
            if (workbook != null)
            {
                try
                {
                    workbook.write(fileOut);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

            fileOut.close();
            excelFilePath = excelFilePath + "/" + fileName;

            String from = ConfigManager.getScalarAdminEmail();
            int index = from.lastIndexOf("\\");
            from = from.substring(index + 1, from.length());
            String to = StringUtils.concatStrings(emailArray, Mailer.EMAIL_SEPARATOR);
            String subject = "USERS EXPIRY INFORMATION REPORT";
            //Setting the PDF and Excel files as attachments.
            mailAttachments = new String[]{excelFilePath};
            if (usersList != null && usersList.size()>1 )
            {
                Mailer.send(from, to, null, null, subject, null, mailAttachments, Mailer.CONTENT_TYPE_HTML, false);
            }
        }
        catch (ScalarException e)
        {
            throw new ScalarException("Could not load Expiry Users Report ", e);
        }
        finally
        {
            if (dbTranasaction != null)
            {
                if (!success)
                {
                    try
                    {
                        dbTranasaction.rollback();
                    }
                    catch (DBXException ignore) {
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws Exception
    {
        new ExpiryUsersReportJob().execute();
    }

}


